package com.atguigu.gmall.wms.listener;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.wms.mapper.WareSkuMapper;
import com.atguigu.gmall.wms.vo.SkuLockVo;
import com.rabbitmq.client.Channel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.List;

@Component
public class StockListener {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private WareSkuMapper wareSkuMapper;

    private static final String KEY_PREFIX = "stock:info:";

    //订单完成 解锁库存 减库存 增加销量的监听器
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("stock.minus.queue"),
            exchange = @Exchange(value = "ORDER.EXCHANGE",type = ExchangeTypes.TOPIC,ignoreDeclarationExceptions = "true"),
            key = {"stock.minus"}
    ))
    public void minus(String orderToken, Channel channel, Message message) throws IOException {

        //判空
        if (StringUtils.isBlank(orderToken)){
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            return;
        }

        //获取锁定信息
        String stockJson = redisTemplate.opsForValue().get(KEY_PREFIX + orderToken);
        if (StringUtils.isBlank(stockJson)){
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            return;
        }

        //获取锁定对象
        List<SkuLockVo> skuLockVos = JSON.parseArray(stockJson, SkuLockVo.class);
        if (CollectionUtils.isEmpty(skuLockVos)){
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            return;
        }

        //遍历锁定信息的集合 解锁 减库存 加销量
        skuLockVos.forEach(skuLockVo->{
            wareSkuMapper.minus(skuLockVo.getCount(),skuLockVo.getWareId());
        });

        //删除redis缓存中的锁定信息
        redisTemplate.delete(KEY_PREFIX+orderToken);

        //确认消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
    //解锁订单的监听器
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("stock.unlock.queue"),
            exchange = @Exchange(value = "ORDER.EXCHANGE",type = ExchangeTypes.TOPIC,ignoreDeclarationExceptions = "true"),
            key = {"order.fail","stock.unlock"}
    ))
    public void unlock(String orderToken, Channel channel, Message message) throws IOException {

        //判空
        if (StringUtils.isBlank(orderToken)){
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            return;
        }

        //获取锁定信息
        String stockJson = redisTemplate.opsForValue().get(KEY_PREFIX + orderToken);
        if (StringUtils.isBlank(stockJson)){
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            return;
        }

        //获取锁定对象
        List<SkuLockVo> skuLockVos = JSON.parseArray(stockJson, SkuLockVo.class);
        if (CollectionUtils.isEmpty(skuLockVos)){
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            return;
        }

        //遍历锁定信息的集合 解锁
        skuLockVos.forEach(skuLockVo->{
            wareSkuMapper.unlock(skuLockVo.getCount(),skuLockVo.getWareId());
        });

        //删除redis缓存中的锁定信息
        redisTemplate.delete(KEY_PREFIX+orderToken);

        //确认消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
